using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Shared;
using System.ServiceModel;

namespace Server
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] 
    internal class DSService : IDSService
    {
        public DataSet1 LoadDataSet(int noOfRows, bool includeSchema, bool binarySerialization)
        {
            return DataSetGenerator.GenerateDS(noOfRows, includeSchema, binarySerialization);
        }

        public Human LoadPlainObject()
        {
            return HumanGenerator.GetHuman();
        }
    }

    public class DSServiceRemoting : MarshalByRefObject, IDSService
    {
        public DataSet1 LoadDataSet(int noOfRows, bool includeSchema, bool binarySerialization)
        {
            return DataSetGenerator.GenerateDS(noOfRows, includeSchema, binarySerialization);
        }

        public Human LoadPlainObject()
        {
            return HumanGenerator.GetHuman();
        }
    }

    internal static class DataSetGenerator
    {
        internal static DataSet1 GenerateDS(int noOfRows, bool includeSchema, bool binarySerialization)
        {
            //Console.WriteLine(noOfRows.ToString());
            
            DataSet1 ds = new DataSet1();

            if (includeSchema)
                ds.SchemaSerializationMode = SchemaSerializationMode.IncludeSchema;
            else
                ds.SchemaSerializationMode = SchemaSerializationMode.IncludeSchema;

            if (binarySerialization)
                ds.RemotingFormat = SerializationFormat.Binary;
            else
                ds.RemotingFormat = SerializationFormat.Xml;

            for (int i = 0; i < noOfRows; i++)
            {
                ds.DataTable1.AddDataTable1Row(123 + i, "something small", "something bigger than small", DateTime.Now);
            }

            return ds;
        }
    }

    internal static class HumanGenerator
    {
        public static Human GetHuman()
        {
            Human human = new Human();
            human.FirstName = "Marcin";
            human.LastName = "Celej";
            human.DateOfBirth = new DateTime(1950, 12, 1);
            human.Single = false;
            human.Description = "He is not single. He is quite old. Do not invite him to a party. He will eat all your food.";
            return human;
        }
    }
}
